using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Purchasing.Security;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager instance = new GameManager();
    
    private void Awake()
    {
        instance = this;
    }
    

    public AudioClip audioClip;
    public AudioSource audioSource;

    private void Start( )
    {
        audioSource.clip = audioClip;
        audioSource.Play();
    }

    private static int id;
    public static float timer = 0f;
    public bool startTimer = true;
    public static float finishTimer = 0f;

    void Update()
    {
        
        
        CheckReset();
        LoadNextSceneBYkey( );
        
        if ( Input.GetKey( KeyCode.Escape ) )
        {
            ExitGame();
        }
        
        if ( SceneManager.GetActiveScene().buildIndex  == 1)
        {
            if ( startTimer )
            {
                timer = 0;
                startTimer = false;
            }
           
            timer += Time.deltaTime;
        }
    }

    public  float GetTimer( )
    {
        return timer;
    }
    public float GetFinishTimer( )
    {
        return finishTimer;
    }
    private void CheckReset()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            ResetScene();
        }
    }
    void ResetScene()
    {
        
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        startTimer = true;
    }

    private void LoadNextSceneBYkey( )
    {
        if ( Input.GetKeyDown( KeyCode.I ) )
        {
            
            LoadNextScene();
        }
    }
    public void LoadNextScene( )
    {
        id = SceneManager.GetActiveScene( ).buildIndex;
        SceneManager.LoadScene((SceneManager.GetActiveScene().buildIndex + 1) % SceneManager.sceneCountInBuildSettings);
        startTimer = true;
    }

    public void LoadFirstScene( )
    {
        id = SceneManager.GetActiveScene( ).buildIndex;
        SceneManager.LoadScene( 0 );
        startTimer = true;
    }
    public void Goal( )
    {
        LoadNextScene();
    }

    public void ExitGame( )
    {
        Application.Quit();
        startTimer = true;
    }
    
    public void FailGame( )
    {
        finishTimer = timer;
        id = SceneManager.GetActiveScene( ).buildIndex;
        SceneManager.LoadScene( SceneManager.sceneCountInBuildSettings - 1 );
        startTimer = true;
        
    }

    public void LoadGameScene(  )
    {
        
        SceneManager.LoadScene( id );
    }
}